package xyz.ddlnt.serviceusr.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import xyz.ddlnt.commonutil.constant.MqConstants;
import xyz.ddlnt.commonutil.constant.StatusConstants;
import xyz.ddlnt.commonutil.util.RandomUtil;
import xyz.ddlnt.model.dto.PageDTO;
import xyz.ddlnt.model.dto.UserContentInteractionDTO;
import xyz.ddlnt.model.entity.PrivateMessageList;
import xyz.ddlnt.model.vo.PrivateMessageListVO;
import xyz.ddlnt.serviceusr.mapper.PrivateMessageListMapper;
import xyz.ddlnt.serviceusr.service.PrivateMessageListService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/26 16:50
 */
@Service
public class PrivateMessageListServiceImpl extends ServiceImpl<PrivateMessageListMapper, PrivateMessageList> implements PrivateMessageListService {
    @Resource
    private PrivateMessageListMapper privateMessageListMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Integer JudgeAndInsertPrivateMessageList(UserContentInteractionDTO userContentInteractionDTO) {
        PrivateMessageList status = privateMessageListMapper.getStatus(userContentInteractionDTO);
        Integer messageVersion = 1;
        if (status == null) {
            PrivateMessageList privateMessageList = PrivateMessageList.builder()
                    .id(RandomUtil.generate12DigitRandomNumber())
                    .userId(userContentInteractionDTO.getUserId())
                    .receiverId(userContentInteractionDTO.getBloggerId())
                    .establishTime(LocalDateTime.now())
                    .build();
            privateMessageListMapper.insert(privateMessageList);
        } else if (Objects.equals(status.getSenderStatus(), StatusConstants.SENDER_DELETED)) {
            LambdaUpdateWrapper<PrivateMessageList> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper
                    .set(PrivateMessageList::getSenderStatus, StatusConstants.SENDER_NORMAL)
                    .set(PrivateMessageList::getMessageVersion, status.getMessageVersion() + 1)
                    .eq(PrivateMessageList::getId, status.getId());
            privateMessageListMapper.update(updateWrapper);
            messageVersion = status.getMessageVersion() + 1;
        }
        return messageVersion;
    }

    @Override
    public List<PrivateMessageListVO> listPrivateMessageList(PageDTO pageDTO) {
        IPage<PrivateMessageListVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        return privateMessageListMapper.selectPrivateMessage(page, pageDTO.getId());
    }

    @Override
    public Boolean deletePrivateMessage(UserContentInteractionDTO userContentInteractionDTO) {
        // 将自己的私信列表删除
        LambdaUpdateWrapper<PrivateMessageList> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(PrivateMessageList::getSenderStatus, StatusConstants.SENDER_DELETED)
                .eq(PrivateMessageList::getId, userContentInteractionDTO.getId());
        privateMessageListMapper.update(updateWrapper);
        // 更新对方的私信列表状态
        rabbitTemplate.convertAndSend(MqConstants.USR_PRIVATE_MESSAGE_LIST, MqConstants.RECEIVER_STATUS_DELETE, userContentInteractionDTO);
        return true;
    }
}
